package guiPackage;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import adapterPackage.GuzergahAdapter;

import systemPackage.LocalStrings;
import systemPackage.MainSystem;


import dataTypesPackage.Guzergah;
import dataTypesPackage.Musteri;
import dataTypesPackage.Satis;
import dataTypesPackage.Sefer;






public class GUI_NewSatis extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2834129789257408629L;
	//variables
	private Satis satis;
	private Musteri.MusteriTypes musteriType;
	private int totalPrice;
	
	
	//Panels
	JPanel startSatisPanel;
	JPanel selectGuzergahPanel;
	JPanel selectSeferPanel;
	JPanel buyTicketsPanel;
	
	//first panels components
	JLabel musteriIDLabel;
	JTextField musteriIDfield;
	JLabel saleType;
	JComboBox saleTypeComboBox;
	
	//second panels components
	JLabel guzergahSelectLabel;
	JComboBox guzergahSelectionComboBox;
	
	//third panels components
	JLabel seferSelectLabel;
	JComboBox seferSelectionComboBox;
	
	//fourth panels components
	JLabel saleStartIDLabel;
	JTextField saleStartIDField;
	JLabel saleEndIDLabel;
	JTextField saleEndIDField;
	
	//compenents used in all panels
	JButton conformationButton;
	
	public GUI_NewSatis(){
		this.setSize(180, 220);
		startSatis();
	}

	private void startSatis() {
		// this is the part that starts sale process.
		startSatisPanel = new JPanel(new GridLayout(5,1));
		musteriIDLabel= new JLabel(LocalStrings.getLocalString(29));
		musteriIDfield = new JTextField();
		saleTypeComboBox = new JComboBox();
		saleTypeComboBox.addItem(LocalStrings.getLocalString(30));							//liquid cargo
		saleTypeComboBox.addItem(LocalStrings.getLocalString(31));							//solid cago
		saleTypeComboBox.addItem(LocalStrings.getLocalString(32));							//person
		saleType = new JLabel(LocalStrings.getLocalString(34));
		conformationButton = new JButton(LocalStrings.getLocalString(33));
		
		ActionListener conformationButtonPress = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// conformation for musteriID and sale type
				try {
					Musteri mus = (Musteri) MainSystem.get(Integer.parseInt(musteriIDfield.getText()), Musteri.class);
					musteriType = mus.getMusteriType();
					//TODO fix hardcoded satisGorevlisiID
					satis = new Satis(01, Integer.parseInt(musteriIDfield.getText()), (String)saleTypeComboBox.getSelectedItem());
					setGuzergah();
				} catch (Exception e2) {
					// if that musteriID is invalid
					JOptionPane.showMessageDialog(null, LocalStrings.getLocalString(43), LocalStrings.getLocalString(44), JFrame.EXIT_ON_CLOSE);
					dispose();
				}

			}
		};
		conformationButton.addActionListener(conformationButtonPress);
		startSatisPanel.add(musteriIDLabel);
		startSatisPanel.add(musteriIDfield);
		startSatisPanel.add(saleType);
		startSatisPanel.add(saleTypeComboBox);
		startSatisPanel.add(conformationButton);
		this.add(startSatisPanel);
		this.setVisible(true);
		
		
	}

	protected void setGuzergah() {
		// makes selection from all guzergahs.

		// clean formerly used components and set window for new usage
		this.remove(startSatisPanel);
		
		conformationButton = new JButton(LocalStrings.getLocalString(36));
		
		guzergahSelectLabel = new JLabel(LocalStrings.getLocalString(37));
		guzergahSelectionComboBox = new JComboBox();
		List<Guzergah> guzergahList = GuzergahAdapter.getAllGuzergah();
		for(Guzergah g:guzergahList){
			guzergahSelectionComboBox.addItem(g);
		}
		ActionListener conformationButtonPress = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// conformation for guzergah
				List<Sefer> seferList = MainSystem.getSeferbyGuzergah((Guzergah)guzergahSelectionComboBox.getSelectedItem());
				selectSefer(seferList);
			}
		};
		conformationButton.addActionListener(conformationButtonPress);
		selectGuzergahPanel = new JPanel(new GridLayout(3,1));
		selectGuzergahPanel.add(guzergahSelectLabel);
		selectGuzergahPanel.add(guzergahSelectionComboBox);
		selectGuzergahPanel.add(conformationButton);
		this.add(selectGuzergahPanel);
		this.setVisible(false);
		this.setVisible(true);		
	}

	protected void selectSefer(List<Sefer> sefer) {
		// makes Selection of sefers.

		// clean formerly used components and set window for new usage
		this.remove(selectGuzergahPanel);
		
		conformationButton = new JButton(LocalStrings.getLocalString(38));
		seferSelectLabel = new JLabel(LocalStrings.getLocalString(39));
		seferSelectionComboBox = new JComboBox();
		for(Sefer s:sefer)
			seferSelectionComboBox.addItem(s);
		
		ActionListener conformationButtonPress = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// conformation for sefer
				satis.setSefer((Sefer)seferSelectionComboBox.getSelectedItem());
				saleSpots();
			}
		};
		conformationButton.addActionListener(conformationButtonPress);
		selectSeferPanel = new JPanel(new GridLayout(3,1));
		selectSeferPanel.add(seferSelectLabel);
		selectSeferPanel.add(seferSelectionComboBox);
		selectSeferPanel.add(conformationButton);
		this.add(selectSeferPanel);
		this.setVisible(false);
		this.setVisible(true);		

	}

	protected void saleSpots() {
		// this step makes selling of selected type.
		
		// clean formerly used components and set window for new usage
		this.remove(selectSeferPanel);
		
		conformationButton = new JButton(LocalStrings.getLocalString(40));
		
		saleStartIDLabel = new JLabel(LocalStrings.getLocalString(41));
		saleStartIDField = new JTextField();
		saleEndIDLabel = new JLabel(LocalStrings.getLocalString(42));
		saleEndIDField = new JTextField();
		ActionListener conformationButtonPress = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// set desired ticket list.
				List<Integer> ticketList = new ArrayList<Integer>();
				for(Integer ticket=Integer.parseInt(saleStartIDField.getText());ticket==Integer.parseInt(saleEndIDField.getText());ticket++){
					ticketList.add(ticket);
				}
				if(satis.getSatisTipi()== LocalStrings.getLocalString(30))
					satis.setSiviKargoAlani(ticketList);
				if(satis.getSatisTipi()== LocalStrings.getLocalString(31))
					satis.setKatiKargoAlani(ticketList);
				if(satis.getSatisTipi()== LocalStrings.getLocalString(30))
					satis.setKamaraList(ticketList);

				if (musteriType == Musteri.MusteriTypes.values()[0])
					totalPrice = satis.getSefer().getFiyatlar().get(0);
				if (musteriType == Musteri.MusteriTypes.values()[1])
					totalPrice = satis.getSefer().getFiyatlar().get(1);
				if (musteriType == Musteri.MusteriTypes.values()[2])
					totalPrice = satis.getSefer().getFiyatlar().get(2);
				if (musteriType == Musteri.MusteriTypes.values()[3])
					totalPrice = satis.getSefer().getFiyatlar().get(3);
				totalPrice= totalPrice*(Integer.parseInt(saleEndIDField.getText()) - Integer.parseInt(saleStartIDField.getText()));
				satis.setTotalPrice(totalPrice);
				int confirm = JOptionPane.showConfirmDialog(null, LocalStrings.getLocalString(45)+totalPrice, LocalStrings.getLocalString(46), JOptionPane.OK_CANCEL_OPTION );
				
				if ( confirm == JOptionPane.OK_OPTION)
					MainSystem.saveNewSatis(satis);

				dispose();
			}
		};
		conformationButton.addActionListener(conformationButtonPress);
		buyTicketsPanel = new JPanel(new GridLayout(5,1));
		buyTicketsPanel.add(saleStartIDLabel);
		buyTicketsPanel.add(saleStartIDField);
		buyTicketsPanel.add(saleEndIDLabel);
		buyTicketsPanel.add(saleEndIDField);
		buyTicketsPanel.add(conformationButton);
		this.add(buyTicketsPanel);
		this.setVisible(false);
		this.setVisible(true);		
	}
	
}
